package com.cskaoyan._02charstream._05buffer;

import java.io.*;

/**
 * @description:
 * @author: 景天
 * @date: 2023/2/20 10:10
 **/
/*
使用缓冲的字符流进行读写数据
 */
public class Demo {
    public static void main(String[] args) throws IOException {
        //writeMethod();

        // 创建输入流对象
        BufferedReader br = new BufferedReader(new FileReader("a.txt"));

        // read
        //char[] chars = new char[1024];
        //int readCount = br.read(chars);
        //System.out.println(new String(chars, 0, readCount));

        // 常规循环读取
        //int readCount;
        //char[] chars = new char[1024];
        //while ((readCount = br.read(chars)) != -1) {
        //    System.out.println(new String(chars, 0, readCount));
        //
        //}


        // BufferedReader中独有的方法
        // readLine(): 读一行, 返回值是字符串, 末尾是null
        // readMethod1(br);

        // 使用BufferedReader进行循环读取
        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }


        // close
        br.close();


    }

    private static void readMethod1(BufferedReader br) throws IOException {
        String s1 = br.readLine();
        System.out.println(s1);
        String s2 = br.readLine();
        System.out.println(s2);
        String s3 = br.readLine();
        System.out.println(s3);
    }

    private static void writeMethod() throws IOException {
        // 创建输出流对象
        //BufferedWriter bw =
        //        new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("a.txt"))));

        BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));
        // write

        bw.write("宝,我吃了草莓蓝莓,蔓越莓,你想我了没?");

        // 自己独有的方法
        // newLine() 换行
        bw.newLine();
        bw.write("哈哈哈哈");

        // flush
        bw.flush();

        // close
        bw.close();
    }
}
